#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework3_8.py
# author:wmf
# datetime:2021/4/6 23:35
# software: PyCharm
'''
this is function description
'''
# import module your need

# 8 在2个文件中存放了英文计算机技术文章
# (可以选择2篇关于Python技术文件操作处理技巧的2篇英文技术文章),
# 请读取文章内容,进行词频的统计;并分别输出统计结果到另外的文件存放;
#     比较这2篇文章的相似度(如果词频最高的前10个词,重复了5个,
#     相似度就是50%;重复了6个,相似度就是60% ,......);

def getText(file): # 解决标点符号和大小写问题
    txt = file.read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_\'{|}~':
        txt = txt.replace(ch, " ")
    return txt

def getWord(txt): # 将单词与单词分开
    txt = txt.split()
    return txt

def getCount(txt): # 获取每个单词的出现次数
    dict1 = {}
    for x in txt:
        if(x in dict1):
            dict1[x] = dict1[x] +1
        else:
            dict1[x] = 1
    return dict1

def keepInfo(dict,file):
    li = sorted(dict.items(),key = lambda items:items[1],reverse = -1)
    file.write(str(li))
    return(li)

with open('python1.txt','r',encoding = 'utf-8') as f1:
    with open('Keep_python1.txt','w',encoding = 'utf-8') as f2:
        dict1 = getCount(getWord(getText(f1)))
        list1 = keepInfo(dict1,f2)

with open('python2.txt','r',encoding = 'utf-8') as f1:
    with open('Keep_python2.txt','w',encoding = 'utf-8') as f2:
        dict2 = getCount(getWord(getText(f1)))
        list2 = keepInfo(dict2,f2)

count = 0

for i in range(0,10):
    for j in range(0,10):
        if(list1[i][0] == list2[j][0]):
            count = count +1

print("相似度为:")
print(count*10,end='%')




